package com.hdd.biz.huodong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.houbb.heaven.util.util.CollectionUtil;
import com.hdd.biz.huodong.dto.ActivityDto;
import com.hdd.biz.huodong.dto.ActivityInfoDto;
import com.hdd.biz.huodong.dto.ActivityListDto;
import com.hdd.biz.huodong.entity.Activity;
import com.hdd.biz.huodong.entity.ActivityPrize;
import com.hdd.biz.huodong.mapper.ActivityMapper;
import com.hdd.biz.huodong.service.ActivityPrizeService;
import com.hdd.biz.huodong.service.ActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.biz.painting.entity.FileInfo;
import com.hdd.dto.SuperResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-07-29
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityPrizeService prizeService;

    @Override
    public SuperResponse list(ActivityListDto listDto) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<Activity> wrapper = new QueryWrapper();
        Page<Activity> pages = new Page<>(listDto.getPageNo(),listDto.getPageSize());
        wrapper.orderByDesc("create_time");
        IPage<Activity> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<Activity> activities = mapIPage.getRecords();
        for(Activity activity:activities){
            activity.setPrizes(prizeService.prizes(activity.getId()));
        }

        response.setItems(activities);
        response.setTotals(pages.getTotal());
        return response;
    }

    @Override
    public SuperResponse info(ActivityDto activityDto) {
        SuperResponse response  = new SuperResponse();
        Activity activity =  baseMapper.selectById(activityDto.getId());
        if(activity!=null){
            activity.setPrizeList(prizeService.prize(activity.getId()));
        }
        response.setItem(activity);
        return response;
    }

    @Override
    public SuperResponse add(ActivityInfoDto activityInfoDto) {
        SuperResponse response = new SuperResponse();
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityInfoDto,activity);
        baseMapper.insert(activity);
        return response;
    }

    @Override
    public SuperResponse update(ActivityInfoDto activityInfoDto) {
        SuperResponse response = new SuperResponse();
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityInfoDto,activity);
        baseMapper.updateById(activity);

        return response;
    }

    @Override
    public SuperResponse stop(ActivityDto activityDto) {
        SuperResponse response = new SuperResponse();
        Activity activity = baseMapper.selectById(activityDto.getId());
        if(!"1".equals(activity.getStatus().toString())){
            response.setMessage("只能对进行种的活动停止");
            response.setCode(-1000);
            return response;
        }
        activity.setStatus(2);
        baseMapper.updateById(activity);
        return response;
    }
}
